/*
 * TimeLabelPanel.java
 *
 * Created on July 11, 2008, 2:26 AM
 */

package anivis.ui.timeline;

import  anivis.base.TimeInterval;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.util.Calendar;
import java.util.Date;
import java.util.GregorianCalendar;
import prefuse.util.TimeLib;
/**
 *
 * @author  urp
 */
public class TimeLabelPanel extends javax.swing.JPanel 
{

    public static int minLabelDist=50;
  
    private TimeInterval  timeSpan=new TimeInterval();
    
    /** Creates new form TimeLabelPanel */
    public TimeLabelPanel() 
    {
        initComponents();        
        
        timeSpan = new TimeInterval( (new GregorianCalendar(1900,1,1)).getTime(),
                                     (new GregorianCalendar(2000,1,1)).getTime() );
    }

    public TimeInterval getTimeSpan() 
    {
        return timeSpan; 
    }
    
    public void setTimeSpan(TimeInterval interval)
    {
        timeSpan = interval;
    }
    
    @Override
    public void paintComponent(Graphics g)
    {
      super.paintComponent(g);
      
      Graphics2D g2d = (Graphics2D) g;
      
              g2d.drawLine(0,0,getWidth(),getHeight());

      int       maxLabels = getWidth()/ minLabelDist;
      GregorianCalendar curCalendar = new GregorianCalendar();
      curCalendar.setTime(timeSpan.getBegin());
      Date beginDate = timeSpan.getBegin();
      Date endDate   = timeSpan.getEnd();
      long timeLength= timeSpan.getLengthInMillis();
      
      //find weather to show year, month or date labels
      int  field=Calendar.DATE;
      long fieldLength = timeSpan.getLength(Calendar.YEAR);
      if(fieldLength>1)
      {
        TimeLib.clearTo(curCalendar,Calendar.MONTH);
        field = Calendar.YEAR;
      }
      else
      { fieldLength = timeSpan.getLength(Calendar.MONTH);
        if(fieldLength>1)
        { 
          TimeLib.clearTo(curCalendar,Calendar.DATE);
          field = Calendar.MONTH;
        }
        else
        { fieldLength = timeSpan.getLength(Calendar.DATE); 
          if(fieldLength>1)
          {
            TimeLib.clearTo(curCalendar,Calendar.HOUR);
            field = Calendar.DATE;
          }
        }
      }
      
      int increment = 1;
      while( fieldLength/increment > maxLabels )
        increment++;
     
      
      while(curCalendar.before(timeSpan.getEnd()))
      {    
        TimeLib.increment(curCalendar, field, increment);
        float pos = getWidth() / ((float)timeLength) * ( (float)(curCalendar.getTimeInMillis()-beginDate.getTime()) );
        System.out.println(pos);
        g2d.drawString("MARK", pos, 10);
        g2d.drawLine((int) pos, 30,(int) pos, 40);
      }
   
   
    }
    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
  // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
  private void initComponents() {

    setName("Form"); // NOI18N

    org.jdesktop.layout.GroupLayout layout = new org.jdesktop.layout.GroupLayout(this);
    this.setLayout(layout);
    layout.setHorizontalGroup(
      layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
      .add(0, 785, Short.MAX_VALUE)
    );
    layout.setVerticalGroup(
      layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
      .add(0, 95, Short.MAX_VALUE)
    );
  }// </editor-fold>//GEN-END:initComponents


  // Variables declaration - do not modify//GEN-BEGIN:variables
  // End of variables declaration//GEN-END:variables

}
